package Test;

import java.util.concurrent.CountDownLatch;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/4/6
 * \* Time: 20:11
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class Demo12 {
    public static void main(String[] args) throws InterruptedException {
        //构造方法的参数表示有几个选手参赛
        CountDownLatch latch = new CountDownLatch(10);

        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(()->{
                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName()+"到达终点！");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }

        //等待所有线程都到达终点
        //当这些线程没有执行完的时候，await就会阻塞，所有的线程都执行完了，await才返回
        latch.await();
        System.out.println("比赛结束！");
    }
}
